//
//  SgyPhotoBrowser.m
//  SGYPhotoPicker
//
//  Created by ShiMac on 15/6/29.
//  Copyright (c) 2015年 guoyan. All rights reserved.
//

#import "SgyPhotoBrowser.h"
#import "SgyPhotoItemView.h"
@interface SgyPhotoBrowser ()
{
    NSMutableDictionary *visiblePhotoItemViewDictM_;            //可视视图
    NSMutableSet        *reusablePhotoItemViewSetM_;            //可重用视图

}
@property (nonatomic)  NSInteger dragPage;
@property (nonatomic)  NSInteger nextPage;
@property (nonatomic)  NSInteger page;
@end

@implementation SgyPhotoBrowser


-(void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor blackColor];
    
    reusablePhotoItemViewSetM_ =[NSMutableSet set];
    visiblePhotoItemViewDictM_=[NSMutableDictionary dictionary];
    
    self.automaticallyAdjustsScrollViewInsets=NO;
    
    self.photoScrollView=[[SgyPhotoScrollView alloc] initWithFrame:self.view.frame];
    self.photoScrollView.contentSize=CGSizeMake(self.view.frame.size.width*self.photosAry.count, 0);
    self.photoScrollView.delegate=self;
    self.photoScrollView.showsHorizontalScrollIndicator=NO;
    self.photoScrollView.showsVerticalScrollIndicator=NO;
//    self.photoScrollView.bounces=YES;
    self.photoScrollView.pagingEnabled=YES;
    [self.view addSubview:self.photoScrollView];
    
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear: animated];
    [self.photoScrollView setContentOffset:CGPointMake(self.photoIndex * self.view.frame.size.width, 0) animated:NO];
    self.nextPage=self.photoIndex;
    [self showPhotoViewAtIndex:self.photoIndex];
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
//    
//    //
////    NSInteger index=ceil(indexFloat);
//    
//  
//    if (self.nextPage<offsetX) {
//        
//        index=self.nextPage/self.view.frame.size.width+1;
//        
//    }else
//    {
//        index=offsetX/self.view.frame.size.width;
//    }
//    
//    if(index<0 && index>self.photosAry.count-1)
//        return;
    
    
    
//
    
    
//    NSUInteger page =
    CGFloat offsetX=scrollView.contentOffset.x;
        NSInteger page=offsetX/self.view.frame.size.width;
    //

    
    //记录dragPage
    if(self.dragPage == 0) self.dragPage = page;
    
    self.page = page;
    [self showPhotoViewAtIndex:self.page];
    
    
    CGFloat pageOffsetX = self.dragPage * scrollView.bounds.size.width;
    
    
    if(offsetX > pageOffsetX){//正在向左滑动，展示右边的页面
        
        if(page >= self.photosAry.count - 1) return;
        
        self.nextPage = page + 1;
        [self showPhotoViewAtIndex:self.nextPage];

    }else if(offsetX < pageOffsetX){//正在向右滑动，展示左边的页面
        
        if(page == 0) return;
        
        self.nextPage = page - 1;
        [self showPhotoViewAtIndex:self.nextPage];

    }
    
    

//
//    if (self.nextPage==0) {
//        self.nextPage=index;
//    }
//    
//    CGFloat X = scrollView.contentOffset.x;
//    
//    CGFloat pageOffsetX = self.nextPage * scrollView.bounds.size.width;
//    
//
//    if (X < pageOffsetX) {
//        NSInteger temp=index-1<0?0:index-1;
//        [self showPhotoViewAtIndex:temp];
//
//    }else if(X>pageOffsetX){
//        NSInteger temp=index+1>self.photosAry.count-1?index:index+1;
//        [self showPhotoViewAtIndex:temp];
//
//    }
    
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate;
{

    self.dragPage=0;
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{

    CGFloat offsetX=scrollView.contentOffset.x;
    NSInteger index=offsetX/self.view.frame.size.width;

    [visiblePhotoItemViewDictM_ enumerateKeysAndObjectsUsingBlock:^(NSValue *key, SgyPhotoItemView *photoItemView, BOOL *stop) {
        
        if(![key isEqualToValue:@(index)]){
            
            [visiblePhotoItemViewDictM_ removeObjectForKey:key];

            [reusablePhotoItemViewSetM_ addObject:photoItemView];
            
        }
    }];

}

-(void)showPhotoViewAtIndex:(NSInteger) index
{
    //已经展示
    if([visiblePhotoItemViewDictM_ objectForKey:@(index)] != nil) return;
    
    SgyPhotoItemView *photoView=[self dequeReusablePhotoItemView];
    if(!photoView){
        photoView=[[SgyPhotoItemView alloc] initWithFrame:CGRectMake(index*self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height)];
        UIImageView *imageView=[[UIImageView alloc] initWithFrame:self.view.frame];

        imageView.tag=100;
        [photoView addSubview:imageView];
        [self.photoScrollView addSubview:photoView];
    }
    
    UIImageView *imageView=(UIImageView *)[photoView viewWithTag:100];
    [[PHCachingImageManager defaultManager] requestImageForAsset:[self.photosAry objectAtIndex:index] targetSize:imageView.frame.size contentMode:PHImageContentModeAspectFill options:nil resultHandler:^(UIImage *result, NSDictionary *info) {
        
        imageView.image=result;
        
    }];
    photoView.photoindex=index;

    [visiblePhotoItemViewDictM_ setObject:photoView forKey:@(index)];
}

-(SgyPhotoItemView *)dequeReusablePhotoItemView{
    
    SgyPhotoItemView *photoItemView = [reusablePhotoItemViewSetM_ anyObject];
    
    if(photoItemView != nil){
        
        //从可重用集合中移除
        [reusablePhotoItemViewSetM_ removeObject:photoItemView];
        UIImageView *imageView=(UIImageView *)[photoItemView viewWithTag:100];
        imageView.image=nil;
    }

    return photoItemView;
}


@end
